﻿#region Using
// System
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

// Csla
using Csla;
#endregion // Using

namespace GACAssemblyExporter.Library
{
    /// <summary>
    /// Version of an assembly
    /// </summary>
    [Serializable()]
    public class VersionInfo : ReadOnlyBase<VersionInfo>
    {
        #region Constants
        /**********************************************************************
         * Constants
         *********************************************************************/
        internal static readonly string SEPARATOR = "__";
        #endregion // Constants

        #region Constructors
        /**********************************************************************
         * Constructors
         *********************************************************************/
        private VersionInfo() 
        {
            this.Id = Guid.Empty;
            this.AssemblyPathList = new List<string>();
        }// of constructor()
        #endregion // Constructors

        #region Properties
        /**********************************************************************
         * Properties
         *********************************************************************/
        public Guid Id { get; private set; }
        public string Name { get; private set; }
        public string AssemblyId { get; private set; }
        public IList<string> AssemblyPathList { get; private set; }
        #endregion // Properties

        #region Data Portal Methods
        /**********************************************************************
         * Data Portal Methods
         *********************************************************************/
        /// <summary>
        /// Data portal fetch method
        /// </summary>
        /// <param name="criteria"></param>
        private void DataPortal_Fetch(RootDirectoryCriteria criteria)
        {
            if (Directory.Exists(criteria.RootDirectory))
            {
                this.Id = Guid.NewGuid();
                DirectoryInfo di = new DirectoryInfo(criteria.RootDirectory);
                string[] dirNameSplit = di.Name.Split(VersionInfo.SEPARATOR.ToCharArray());
                if (dirNameSplit.Length >= 2)
                {
                    this.Name = dirNameSplit[0];
                    if (!string.IsNullOrEmpty(dirNameSplit[1]))
                    {
                        this.AssemblyId = dirNameSplit[1];
                    }// if the value is set
                    else if(dirNameSplit.Length >= 3)
                    {
                        this.AssemblyId = dirNameSplit[2];
                    }// if there are enough values
                }// if the split is right

                if (this.AssemblyPathList == null) { this.AssemblyPathList = new List<string>(); }

                foreach(string assemblyPath in Directory.GetFiles(criteria.RootDirectory, "*.dll"))
                {
                    this.AssemblyPathList.Add(assemblyPath);
                }// for each file
            }// if the directory exists
        }// of DataPortal_Fetch(RootDirectoryCriteria)
        #endregion // Data Portal Methods

        #region Methods
        /**********************************************************************
         * Methods
         *********************************************************************/
        /// <summary>
        /// Export all assemblies from this version to the outout directory
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public bool ExportToDirectory(string directory, bool overrwrite)
        {
            bool toReturn = true;

            foreach (string assembly in this.AssemblyPathList)
            {
                FileInfo fi = new FileInfo(assembly);
                string newFilename = Path.Combine(directory, fi.Name);
                File.Copy(assembly, newFilename, overrwrite);
                toReturn = File.Exists(newFilename) && toReturn;
            }// for each assembly

            return toReturn;
        }// of bool ExportToDirectory(string, bool)

        public bool ExportToDirectory(string directory)
        {
            return this.ExportToDirectory(directory, true);
        }// of bool ExportToDirectory(string)
        #endregion // Methods

        #region Factory Methods
        /**********************************************************************
         * Factory Methods
         *********************************************************************/
        /// <summary>
        /// Gets the version info
        /// </summary>
        /// <param name="versionDir"></param>
        /// <returns></returns>
        internal static VersionInfo Get(string versionDir)
        {
            return DataPortal.Fetch<VersionInfo>(new RootDirectoryCriteria() { RootDirectory = versionDir });
        }// of VersionInfo Get(string)
        #endregion // Factory Methods
    }// of class Version
}// of namespace GACAssemblyExporter.Library